package com.efounder.util;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

import java.lang.reflect.Field;

/**
 * @author fengshuquan
 */

public class ToastUtil {
    private static Field sFieldTN;
    private static Field sFieldTNHandler;


    public ToastUtil() {
    }

//
//    static {
//        //解决一个问题
//        //https://blog.csdn.net/qq_31339141/article/details/90108049?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
//        try {
//            sFieldTN = Toast.class.getDeclaredField("mTN");
//            sFieldTN.setAccessible(true);
//            sFieldTNHandler = sFieldTN.getType().getDeclaredField("mHandler");
//            sFieldTNHandler.setAccessible(true);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static void hook(Toast toast) {
//        try {
//            Object tn = sFieldTN.get(toast);
//            Handler preHandler = (Handler) sFieldTNHandler.get(tn);
//            sFieldTNHandler.set(tn, new SafelyHandlerWrapper(preHandler));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static class SafelyHandlerWrapper extends Handler {
//        private Handler impl;
//
//        SafelyHandlerWrapper(Handler impl) {
//            this.impl = impl;
//        }
//
//        @Override
//        public void dispatchMessage(Message msg) {
//            try {
//                super.dispatchMessage(msg);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//
//        @Override
//        public void handleMessage(Message msg) {
//            impl.handleMessage(msg);
//        }
//    }
//

    private static Toast toast = null;

    @Deprecated
    public static void showToast(Context context, final String msg) {
        showCommonToast(context, msg);
    }

    @Deprecated
    public static void showToast(final Context context, final int resId) {
        showCommonToast(context, AppContext.getInstance().getResources().getString(resId));
    }

    public static void showToast(final int resId) {
        showCommonToast(AppContext.getInstance(), AppContext.getInstance().getResources().getString(resId));
    }

    public static void showToast(String msg) {
        showCommonToast(AppContext.getInstance(), msg);
    }

    private static void showCommonToast(final Context context, final String msg) {
        if (context == null) {
            return;
        }
        //android9.0解决了toast重复显示的问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            toast = getSingleToast(msg);

            toast.show();
        }
    }


    /**
     * 显示Toast【居中】
     *
     * @param msg 显示的内容-字符串
     */
    public static void showToastCenter(String msg) {
        if (AppContext.getInstance() != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                Toast centerToast = Toast.makeText(AppContext.getInstance(), msg, Toast.LENGTH_SHORT);
                centerToast.setGravity(Gravity.CENTER, 0, 0);
                centerToast.show();
            } else {

                toast = getSingleToast(msg);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    }


    private static Toast getSingleToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(AppContext.getInstance(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        return toast;
    }

    /**
     * 显示Toast【居上】
     *
     * @param msg 显示的内容-字符串
     */
    public static void showShortToastTop(String msg) {
        if (AppContext.getInstance() != null) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                Toast centerToast = Toast.makeText(AppContext.getInstance(), msg, Toast.LENGTH_SHORT);
                centerToast.setGravity(Gravity.CENTER, 0, 0);
                centerToast.show();
            } else {

                toast = getSingleToast(msg);
                toast.setGravity(Gravity.TOP, 0, 0);
                toast.show();
            }
        }
    }

}
